package com.sxintelbox

import android.os.Bundle
import android.os.Message
import com.facebook.react.ReactActivity
import com.facebook.react.ReactActivityDelegate
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
import com.facebook.react.defaults.DefaultReactActivityDelegate
import android.os.Handler
import android.os.Handler.Callback
import android.view.View
import com.facebook.react.modules.core.DeviceEventManagerModule

class MainActivity : ReactActivity() {

  /**
   * Returns the name of the main component registered from JavaScript. This is used to schedule
   * rendering of the component.
   */
  override fun getMainComponentName(): String = "SXIntelBox"

  /**
   * Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate]
   * which allows you to enable New Architecture with a single boolean flags [fabricEnabled]
   */
  override fun createReactActivityDelegate(): ReactActivityDelegate =
      DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled)

    private fun sendEvent(eventName: String, params: String) {

        println("发送消息成功$eventName$params")
        reactDelegate?.reactRootView?.currentReactContext?.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
            ?.emit(eventName, params)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(null)

        //创建绑定对象并绑定服务，用于定时重启app
//        val intent = Intent(this, RestartAppService::class.java)
//        bindService(intent, connService, BIND_AUTO_CREATE)


        val decorView = window.decorView
        val uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN
        decorView.systemUiVisibility = uiOptions
    }

    /**
     * ServiceConnection代表与服务的连接，它只有两个方法，
     * onServiceConnected和onServiceDisconnected，
     * 前者是在操作者在连接一个服务成功时被调用，而后者是在服务崩溃或被杀死导致的连接中断时被调用
     */
//    private var myService: RestartAppService? = null
//    private val connService: ServiceConnection = object : ServiceConnection {
//        /**
//         * Called when a connection to the Service has been established,
//         * with the android.os.IBinder of the communication channel to the Service.
//         */
//        /**
//         * 与服务器端交互的接口方法 绑定服务的时候被回调，在这个方法获取绑定Service传递过来的IBinder对象，
//         * 通过这个IBinder对象，实现宿主和Service的交互。
//         */
//        override fun onServiceConnected(name: ComponentName, service: IBinder) {
//            val mBinder = service as MyBinder
//            myService = mBinder.service
//            mBinder.startRestartTask(this@MainActivity)
//        }
//
//        override fun onServiceDisconnected(name: ComponentName) {
//            myService = null
//        }
//    }

    override fun onResume() {
        super.onResume()
        resetDisconnectTimer()
    }

    override fun onStop() {
        super.onStop()
        stopDisconnectTimer()
    }

    override fun onUserInteraction() {
        super.onUserInteraction()
        resetDisconnectTimer();
        println("有人交互，发送消息")
        sendEvent("playIdleScreen", "");
    }

    val disconnectCallback = Runnable {
        // Perform any required operation on disconnect
        println("无人交互，发送消息")
        sendEvent("playIdleScreen", "playIdleScreen");
    }
    var disconnectHandler = Handler(Callback { msg: Message ->
        // Handle message
        true
    })

    fun resetDisconnectTimer() {
        disconnectHandler.removeCallbacks(disconnectCallback)
        disconnectHandler.postDelayed(disconnectCallback, 3 * 60 * 1000)
    }

    fun stopDisconnectTimer() {
        disconnectHandler.removeCallbacks(disconnectCallback)
    }
}
